package com.code.modules.base.controller;

import com.code.common.utils.PageUtil;
import com.code.config.Swagger.params.ApiJsonObject;
import com.code.config.Swagger.params.ApiJsonProperty;
import com.code.modules.base.model.Role;
import com.code.common.utils.ToolUtil;
import com.code.common.vo.ResultData;
import com.code.modules.base.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/role")
@Api(value = "角色控制器", tags = {"角色接口"})
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation("角色列表")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @RequiresAuthentication
    @RequiresPermissions("role:allList")
    @PostMapping("/allList")
    public ResultData allList() {
        List<Role> rolelist = roleService.selectList(null);
        if(rolelist!=null){
            return new ResultData().succ(200,"查询成功",rolelist);
        }else{
            return new ResultData().fail(400,"查询失败");
        }

    }


    @ApiOperation("获取该用户的角色组")
    @RequiresAuthentication
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/getroleidlist")
    public ResultData getRoleidlist(@ApiJsonObject(name = "Role_getRoleidlist_Map", value = {
            @ApiJsonProperty(key = "userid",  example = "1",  description = "用户ID", type = int.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer userid = (Integer)params.get("userid");
        List<Role> roles = roleService.findRoleByuserid(userid);
        List<Integer> roleidlist = new ArrayList<Integer>();
        for(Role r : roles) {
            roleidlist.add(r.getId());
        }
        return new ResultData().succ(200,"查询成功", roleidlist);
    }




    @RequiresAuthentication
    @RequiresPermissions("role:pageList")
    @ApiOperation("角色分页")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/pageList")
    public ResultData pageList(@ApiJsonObject(name = "Role_pageList_Map", value = {
            @ApiJsonProperty(key = "currentPage",  example = "1",  description = "当前页", type = int.class, required = true),
            @ApiJsonProperty(key = "size",  example = "10",  description = "每页数量", type = int.class, required = true),
            @ApiJsonProperty(key = "key",  example = "查询内容",  description = "查询内容", type = String.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer currentPage = (Integer) params.get("currentPage");
        Integer size = (Integer) params.get("size");
        String key = (String) params.get("key");
        List<Role> rlist = roleService.getPageList(key);
        if(rlist != null) {
            PageUtil<Role> rpage = new PageUtil<Role>();
            rpage.queryPager(currentPage,size,rlist);
            return new ResultData().succ(200,"查询成功",rpage);
        }
        return new ResultData().fail(400,"查询失败");

    }

    @RequiresAuthentication
    @RequiresPermissions("role:add")
    @ApiOperation("添加角色")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/add")
    public ResultData addRole(@RequestBody Role role) {
        if(ToolUtil.isNotEmpty(role)) {

            if(roleService.checkRolecode(role.getCode()) == true) {
                role.setCreatetime(new Date());
                role.setUpdatetime(new Date());
                roleService.insert(role);
                return new ResultData().succ(200,"添加成功");
            } else {
                return new ResultData().fail(400,"角色权限编码已存在");
            }
        }
        return new ResultData().fail(400,"添加失败");
    }

    @RequiresAuthentication
    @RequiresPermissions("role:edit")
    @ApiOperation("修改角色")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/edit")
    public ResultData editRole(@RequestBody Role role) {
        if(ToolUtil.isNotEmpty(role)) {
            role.setUpdatetime(new Date());
            roleService.updateById(role);
            return new ResultData().succ(200,"更新成功");
        }
        return new ResultData().fail(400,"更新失败");
    }

    @RequiresAuthentication
    @RequiresPermissions("role:del")
    @ApiOperation("删除角色")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/del")
    public ResultData delRole(@ApiJsonObject(name = "Role_delRole_Map", value = {
            @ApiJsonProperty(key = "roleId",  example = "1",  description = "角色ID", type = int.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer roleId =  (Integer) params.get("roleId");
        boolean flag =  roleService.deleteById(roleId);
        if(flag == true) {
            return new ResultData().succ(200,"删除成功");
        }
        return new ResultData().fail(400,"删除失败");
    }


    @RequiresAuthentication
    @RequiresPermissions("role:setauthority")
    @ApiOperation("分配资源")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/setauthority")
    public ResultData setAuthority(@ApiJsonObject(name = "Role_setAuthority_Map", value = {
            @ApiJsonProperty(key = "roleid",  example = "1",  description = "角色ID", type = int.class, required = true),
            @ApiJsonProperty(key = "resids", example = "[1,2,3,4,5,6]", description = "资源组", type = String.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer roleid = (Integer) params.get("roleid");
        List<Integer> resids = (List<Integer>)params.get("resids");
        roleService.setAuthority(roleid,resids);
        return new ResultData().succ(200,"分配资源成功");
    }





}

